package 数据结构;

import java.util.HashMap;
import java.util.Map;

public class LeetCode209_单词规律 {
    public boolean wordPattern(String pattern, String s) {
        //把s里的字符串用中间的空格分割成字符数组
        String[] words = s.split(" ");
        //字符和单词是相互映射，数量必须相等
        if(words.length != pattern.length()){
            return false;
        }
        Map<Object, Integer> map = new HashMap<>();
        for (Integer i = 0;i < words.length; i++){
            if (map.put(pattern.charAt(i), i) != map.put(words[i], i)){
                return false;
            }
        }
        return true;
    }
}
